<template>
  <el-form
    ref="registerForm"
    label-width="100px"
    class="registerForm sign-in-form"
    :model="registerUser"
    :rules="registerRules"
  >
    <el-form-item label="用户名" prop="name">
      <el-input
        v-model="registerUser.name"
        placeholder="Enter name..."
      ></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input
        v-model="registerUser.email"
        placeholder="Enter email..."
      ></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input
        v-model="registerUser.password"
        placeholder="Enter password..."
        type="password"
      ></el-input>
    </el-form-item>
    <el-form-item label="确认密码" prop="password2">
      <el-input
        v-model="registerUser.password2"
        placeholder="Enter password..."
        type="password"
      ></el-input>
    </el-form-item>
    <el-form-item label="选择身份">
      <el-select
        v-model="registerUser.role"
        placeholder="请选择身份"
        style="width: 100%"
      >
        <el-option label="管理员" value="admin"></el-option>
        <el-option label="用户" value="user"></el-option>
        <el-option label="游客" value="visitor"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button
        type="primary"
        class="submit-btn"
        @click="handleRegister('registerForm')"
        >注册</el-button
      >
    </el-form-item>
    <!-- 找回密码 -->
    <div class="tiparea">
      <p>忘记密码?<a href="">立即找回</a></p>
    </div>
  </el-form>
</template>
<script lang="ts">
import { ref, getCurrentInstance } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
export default {
  name: 'RegisterForm',
  props: {
    registerUser: {
      type: Object,
      required: true,
    },
    registerRules: {
      type: Object,
      required: true,
    },
  },
  setup(props: any) {
    // @ts-ignore
    const { ctx } = getCurrentInstance()
    // 获取router
    const router = useRouter()
    const signUpMode = ref<Boolean>(false)
    // 登录事件
    const handleRegister = (formName: String) => {
      console.log(formName)
      console.log(ctx)
      ctx.$refs[formName].validate((valid: Boolean) => {
        if (valid) {
          console.log(111111111111, ctx.$axios)
          ctx.$refs[formName].$axios
            .post('/api/v1/auth/register', props.registerUser)
            .then((res: any) => {
              ElMessage({
                message: '注册成功',
                type: 'success',
              })
              //   路由跳转
              router.push('/')
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
    return {
      signUpMode,
      handleRegister,
    }
  },
}
</script>
<style scoped>
/* form */
.loginForm {
  margin-top: 20px;
  background-color: #fff;
  padding: 20px 40px 20px 20px;
  border-radius: 5px;
  box-shadow: 0px 5px 10px #cccc;
}

.submit-btn {
  width: 100%;
}
.tiparea {
  text-align: right;
  font-size: 12px;
  color: #333;
}
.tiparea p a {
  color: #409eff;
}
</style>